﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Newtonsoft.Json;

namespace WebApplication.Admin.api
{
    public abstract class BaseApiPro<M> : System.Web.UI.Page
        where M : class, new()
    {

        protected abstract BaseServicePro<M> GetService();

        protected abstract string ListOrderBy();

        public virtual void Delete()
        {
            string id = Request.Form["ids"];

            if (string.IsNullOrEmpty(id))
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = -1,
                    msg = "参数错误"
                }));
                return;
            }

            int[] ids = JsonConvert.DeserializeObject<int[]>(id);

            if (GetService().Delete(ids))
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = 0,
                    msg = "删除数据成功"
                }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = -1,
                    msg = "删除数据失败"
                }));
            }
        }

        public virtual void List()
        {
            int totalCount = 0;


            int page = Convert.ToInt32(Request.Form["page"]);
            int limit = Convert.ToInt32(Request.Form["limit"]);
            string searchParams = Request.Form["searchParams"];


            List<M> list = GetService().GetList(searchParams, ref totalCount, page, limit);

            string orderby = ListOrderBy();
            if (!string.IsNullOrEmpty(orderby))
            {
                list = GetService().GetList(searchParams, orderby, ref totalCount, page, limit);
            }

            Response.Write(JsonConvert.SerializeObject(new
            {
                code = 0,
                count = totalCount,
                msg = "",
                data = list
            }));

        }

        public virtual void GetOne()
        {

            string searchParams = Request.Form["searchParams"];

            if (string.IsNullOrEmpty(searchParams))
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = -1,
                    msg = "参数错误"
                }));
                return;
            }

            M data = GetService().GetOne(searchParams);

            Response.Write(JsonConvert.SerializeObject(new
            {
                code = 0,
                data = data
            }));
        }

        public virtual void Save()
        {
            M model = new Utils.Model<M>().GetModel(Request.Form);

            if (GetService().Save(model))
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = 0,
                    msg = "插入数据成功"
                }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = -1,
                    msg = "插入数据失败"
                }));
            }
        }

        public virtual void Update()
        {
            M model = new Utils.Model<M>().GetModel(Request.Form);

            if (GetService().Update(model))
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = 0,
                    msg = "更新数据成功"
                }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new
                {
                    code = -1,
                    msg = "更新数据失败"
                }));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            Response.ContentType = "application/json; charset=utf-8";

            if (Session["USER"] == null || ((Models.User)Session["USER"]).Role != 2)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "无权限操作！" }));
                return;
            }

            if (Request.Form["action"] == null)
            {
                Response.StatusCode = 400;
                Response.Write(JsonConvert.SerializeObject(new { code = 409, msg = "请求方法错误！" }));
                return;
            }

            //反射执行该方法
            var method = GetType().GetMethod(Request.Form["action"]);
            method.Invoke(this, null);
        }
    }
}